package com.jack.vipmis.vip.api.web.controller;

import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.jack.vipmis.common.web.controller.BaseController;
import com.jack.vipmis.vip.api.entity.DiscountEntity;
import com.jack.vipmis.vip.api.util.MsgUtil;
import com.jack.vipmis.vip.card.entity.Discount;
import com.jack.vipmis.vip.card.service.DiscountService;

/**
 * 优惠券管理
 * 
 * @author JackLiang
 * @date 2014-7-7
 * @version 1.0
 */
@Controller
@RequestMapping(value = "api/discount")
public class DiscountApiController extends BaseController<Discount, Long> {

	@Autowired
	private DiscountService	discountService;

	/**
	 * 根据ID获取优惠券
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "find", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> find(@RequestParam("id") Long id) {
		try {
			Discount d = discountService.findOne(id);
			return MsgUtil.doneOk(d);
		} catch (Exception e) {
		}
		return MsgUtil.doneFail();
	}

	/**
	 * 分页查询
	 * 
	 * @param page
	 * @param pageSize
	 * @param type
	 * @param bsId
	 * @param x
	 * @param y
	 * @param con
	 * @param text
	 *            1表示按坐标来查询2表示根据下载最多3最新上传排序4表示根据name来查询0表示默认
	 * @return
	 */
	@RequestMapping(value = "findPage", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> findByPage(@RequestParam("page") int page, @RequestParam("pageSize") int pageSize,
			@RequestParam("type") int type, @RequestParam("bsId") long bsId, @RequestParam("x") String x,
			@RequestParam("y") String y, @RequestParam("con") String con, HttpServletRequest request) {
		try {
			String text = request.getParameter("text") == null ? "" : //
					URLDecoder.decode(request.getParameter("text"), "UTF-8");
			
			Page<Discount> result = discountService.getDiscount(page, pageSize, type, bsId, x, y, con, text);
			List<Discount> list = result.getContent();
			List<DiscountEntity> resultList = Lists.newArrayList();

			for (int i = 0, len = list.size(); i < len; i++) {
				DiscountEntity d = new DiscountEntity();
				d.setId(list.get(i).getId());
				d.setBsdistanceY(list.get(i).getBsdistanceX() == null ? "" : list.get(i).getBsdistanceY());
				d.setBsdistanceY(list.get(i).getBsdistanceY() == null ? "" : list.get(i).getBsdistanceY());
				d.setBusiness(list.get(i).getBusiness());
				d.setCreateTime(list.get(i).getCreateTime());
				d.setDisConsume(list.get(i).getDisConsume() == null ? "" : list.get(i).getDisConsume());
				d.setDisDownLoadCount(list.get(i).getDisDownLoadCount() == null ? 0 : list.get(i).getDisDownLoadCount());
				d.setDisImageContent(list.get(i).getDisImageContent() == null ? "" : list.get(i).getDisImageContent());
				d.setDisTitle(list.get(i).getDisTitle() == null ? "" : list.get(i).getDisTitle());
				d.setDisSpecialTips(list.get(i).getDisSpecialTips() == null ? "" : list.get(i).getDisSpecialTips());
				d.setDisPrice(list.get(i).getDisPrice() == null ? 0D : Double.valueOf(list.get(i).getDisPrice()));
				d.setDisLogo(list.get(i).getDisLogo() == null ? "" : list.get(i).getDisLogo());
				d.setDisValidityTime(list.get(i).getDisValidityTime() == null ? 0L : list.get(i).getDisValidityTime());
				d.setDisType(list.get(i).getDisType() == null ? 0 : list.get(i).getDisType());
				d.setBsId(list.get(i).getBusiness() == null ? 0L : list.get(i).getBusiness().getId());

				resultList.add(d);
			}
			return MsgUtil.doneOk(resultList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return MsgUtil.doneFail();
	}

}
